<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Gallery_db
 *
 * @author francesco
 */
class Gallery_db {
    //put your code here
   /**
     * this method save the data into the db for club images,
     * if return false there is no error
     *
     * @param $image the handle of image
     * @return <bool>
     *
     */
    public function setImageData($image){

        $table='immagine_club';
        try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();
            $db->insert($table, $image);
            //print_r($image);
            // i close the connection
            $db->closeConnection();
            $err = false;
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
            $err = true;
        }
        return $err;
    }

    /**
     * getImageDataByPiva preleva dal db tutte le info riguardanti tutte
     * le immagini associate ad un club (piva): il path, il thumb_path,
     * l'alt e il title.
     *
     * @param <varchar> $piva
     * @return <string>
     */
    public function getImageDataByPiva($piva){

    $table='immagine_club';
    try {
        require_once "models/Db/Db_Db.php";

        $db = Db_Db::conn();
        $statement="SELECT img_path, alt, title,img_thumb FROM immagine_club WHERE piva='" .$piva. "'";
        $club_img=$db->fetchAll($statement);
     
     //   var_dump($club_img);
        if (!$club_img) {
    //        throw new Exception("Could not find row $piva");
        }
        $db->closeConnection();
        
        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
    return $club_img;
    }


    /**
     * deleteImageByPath(), permette di cancellare dal db le informazioni
     * riguardanti un'immagine.
     *
     * @param <string> $path
     * @return <bool>
     */
    public function deleteImageByPath($path){

    $table='immagine_club';
    try {
        require_once "models/Db/Db_Db.php";

        $db = Db_Db::conn();
        //$statement="DELETE FROM immagine_club WHERE img_path='" .$path. "'";
         $condition = array(
            'img_path = ?' => $path
        );


        $delete=$db->delete($table, $condition);

       //var_dump($delete);
        if (!$delete) {
            echo "error in db.";
    //        throw new Exception("Could not find row $piva");
        }
        $db->closeConnection();

        } catch(Zend_Db_Exception $e) {
            echo $e->getMessage();
        }
    return $delete;
    }

    /**
     * this method is used to get the path of the club image given his piva
     * @param <type> $piva
     * @return <type>
     */
    public function getDiscoImgPath($piva){
        try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();
            $statement="SELECT full_path FROM immagine_disco WHERE piva='" . $piva . "'";
            
            //$disco=$db->fetchAll($statement);
            //$disco=$disco[0];
            $disco=$db->fetchOne($statement);

            $db->closeConnection();
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
        }

        return $disco;
    }

    /**
     *
     * @param <type> $piva
     * @return <type>
     */
    public function getDiscoImgThumbPath($piva){
        try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();
            $statement="SELECT thumb_path FROM immagine_disco WHERE piva='" . $piva . "'";

            $disco=$db->fetchOne($statement);

            $db->closeConnection();
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
        }

        return $disco;
    }
    /*
    public function getClubLogoPath($piva){
        try {

            require_once "models/Db/Db_Db.php";

            $db = Db_Db::conn();

            $statement="SELECT full_path FROM immagine_disco WHERE piva='" . $piva . "'";

            $disco=$db->fetchAll($statement);
            $disco=$disco[0];

            // i close the connection
            $db->closeConnection();
        } catch (Zend_Db_Exception $e) {
            echo $e->getMessage();
        }

        return $disco;
    }
    */
}
?>